package com.example.springboot.controller;


import com.example.springboot.pojo.Equipment;
import com.example.springboot.pojo.WorkOrder;
import com.example.springboot.service.EquipmentServiceImpl;
import com.example.springboot.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Equipment")
public class EquipmentController {
    @Autowired
    @Qualifier("equipmentService")
    EquipmentServiceImpl equipmentService;

    @RequestMapping("/addEquipment")
    public String addEquipment(Model model, Equipment equipment){
        boolean flag = equipmentService.addEquipment(equipment);
        model.addAttribute("flag",flag);
        List<Equipment> equipmentList = equipmentService.getEquipmentList();
        Page<Equipment> page = new Page<>(0,8,equipmentList);
        model.addAttribute("page",page);
        System.out.println(page.dataList);
        return "common";
    }
    @RequestMapping("/showUpdateEquipment/{id}")
    public String showUpdateEquipment(Model model,@PathVariable int id){
        Equipment equipment = equipmentService.queryById(id);
        model.addAttribute("equipment",equipment);
        return "updateEquipment";
    }

    @RequestMapping("/showAddEquipment")
    public String showAddEquipment(Model model){
        return "addEquipment";
    }
    @RequestMapping("/getEquipmentList/{pageNum}")
    public String getEquipmentList(Model model,@PathVariable int pageNum,HttpSession session){
        List<Equipment> equipmentList = equipmentService.getEquipmentList();
        Page<Equipment> page = new Page<>(pageNum,8,equipmentList);
        model.addAttribute("page",page);
        session.setAttribute("currentPage",page.currentPage);
        System.out.println(page.dataList);
        return "common";
    }

    @RequestMapping("/queryById/{id}")
    public void queryById(Model model,@PathVariable int id){
        Equipment equipment = equipmentService.queryById(id);
        System.out.println(equipment);
        model.addAttribute("equipment",equipment);
    }

    @RequestMapping("/deleteById/{id}")
    public String deleteById(Model model,@PathVariable int id,HttpSession session){
        boolean flag = equipmentService.deleteById(id);
        System.out.println(id);
        model.addAttribute("flag",flag);
        return "redirect:/Workshop/getEquipmentList/"+session.getAttribute("currentPage");
    }

    @RequestMapping("/updateById/{id}")
    public String updateById(Model model,Equipment equipment,HttpSession session,@PathVariable int id){
        System.out.println("updateById="+id);
        equipment.setId(id);
        boolean flag = equipmentService.updateById(equipment);
        model.addAttribute("flag",flag);
        return "redirect:/Workshop/getEquipmentList/"+session.getAttribute("currentPage");
    }

    @RequestMapping("/getEquipmentListByCondition/{pageNum}")
    public String getEquipmentListByCondition(Model model, String name, String state, @PathVariable int pageNum, HttpSession session){
        Map<String,String> map=new HashMap<>();
        if(state.isEmpty())
            state=null;
        if(name.isEmpty())
            name=null;
        map.put("name",name);
        map.put("state",state);
        List<Equipment> equipmentList = equipmentService.getEquipmentListByCondition(map);
        //System.out.println(equipmentList);
        Page<Equipment> page = new Page<>(pageNum,8,equipmentList);
        model.addAttribute("page",page);
        session.setAttribute("currentPage",page.currentPage);
        return "equipmentListByCon";
    }

    @RequestMapping("/getWorkOrderListById/{pageNum}/{id}")
    public String getWorkOrderListById(Model model, @PathVariable int id, @PathVariable int pageNum, HttpSession session){
        List<WorkOrder> workOrderList = equipmentService.getWorkOrderListById(id);
        Page<WorkOrder> page = new Page<>(pageNum,8,workOrderList);
        model.addAttribute("page",page);
        session.setAttribute("currentPage",page.currentPage);
        session.setAttribute("eqid",id);
        return "workOrderList";
    }
    @RequestMapping("/getWorkOrderListById/{pageNum}")
    public String getWorkOrderListById2(Model model, @PathVariable int pageNum, HttpSession session){
        int id= (int) session.getAttribute("eqid");
        List<WorkOrder> workOrderList = equipmentService.getWorkOrderListById(id);
        Page<WorkOrder> page = new Page<>(pageNum,8,workOrderList);
        model.addAttribute("page",page);
        session.setAttribute("currentPage",page.currentPage);
        return "workOrderList";
    }
}
